/**
 * Created by hiephn on 2014/08/26.
 */
define(['durandal/app','jquery', 'shell','plugins/router', 'facebook'],function (app, $, shell, router) {
    var AUTH_URL = app.conf.MNP_BACK_URL + 'security/auth',
        self = null;

    var VM = {
        username: app.koo(),
        password: app.koo(),
        activate: function() {
            self = this;
            shell.hasHeader(false);
            shell.hasSidebar(false)
        },
        canDeactivate: function() {
            shell.hasHeader(true);
            shell.hasSidebar(true);
            return true;
        },
        login: function() {
            app.http.post(AUTH_URL, {}, {
                headers: {
                    Authorization: 'Basic ' + btoa(self.username() + ':' + self.password())
                },
                success: function(json) {
                    router.navigate('channel-select');
                }
            });
        },
        fbLogin: function() {
            FB.login(function(response) {
                if (response.authResponse) {
                    FB.api('/me?fields=name,email,gender,birthday,updated_time,picture.height(32)', function(response) {
                        console.log(response);
                        app.user({fb: response});
                        router.navigate('channel-select');
                    });
                } else {
                    console.log('FB: User cancelled login or did not fully authorize.');
                }
            }, {scope: 'email,user_birthday,user_location,user_likes,user_friends'});
        }
    };
    return VM;
});
